package ua.eug.airv1.ground;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;

import ua.eug.airv1.controllers.ScreenController;
import ua.eug.airv1.map.Tile;
import ua.eug.airv1.map.TilePos;
import ua.eug.airv1.orders.WeaponOrder;
import ua.eug.airv1.utils.Config;
import ua.eug.airv1.utils.EDirection;
import ua.eug.airv1.weapon.IWeapon;
import ua.eug.airv1.weapon.ZWeapon;

public abstract class GroundWeapon extends Actor implements IWeapon {

	protected TextureRegion texture;
	private final float width = Config.TileWidth;
	private final float heigth = Config.TileHeight;
	
	public Boolean alive = false;
	private int owner;
	public TilePos position;
	protected Tile place = Tile.ZTile;
	protected WeaponOrder order;
	
	public GroundWeapon(int owner, Tile tile) {
		super();
		setPosition(tile.pos);
		place = tile;
		tile.setGroundWeapon(this);
		this.owner = owner;
	}

	public void setPosition(TilePos xy) {
		this.position = xy;
		this.setPosition();
	}

	public void setPosition() {
		Vector2 p = ScreenController.getInstance().XyToStage(position .x,position .y);
		this.setPosition(p.x, p.y);
	}

	@Override
    public void draw(Batch batch, float alpha){
		if (alive)
			batch.draw(texture,getX(), getY(),
        		width/2,heigth/2, width,heigth, 1,1, getRotation());
    }

	
	//======== IWeapon ====================
	@Override
	public Boolean doAirDamage(IWeapon killer) {
		return false;
	}
	@Override
	public Boolean doGroundDamage(IWeapon killer) {
		alive = false;
		place.setGroundWeapon(ZWeapon.Nobody);
		place = Tile.ZTile;
		return true;
	}
	
	@Override
	public int getOwner() {
		return owner;
	}

	@Override
	public Boolean aniMove(float seconds) {
		// TODO 
		return false;
	}

	@Override
	public void setTile(Tile t) {
		place = t;
		t.setGroundWeapon(this);
		setPosition();
	}
	@Override
	public Tile getTile() {
		return place;
	}

	@Override
	public EDirection getDirection() {
		return EDirection.N;
	}

	@Override
	public WeaponOrder getOrder() {
		return order;
	}

	public int getNMoves() {
		return 0;
	}

}
